/**
 * 
 */
package evolution;

import ca.swank.hardquor.Move;
import ca.swank.hq.ai.CachingSmartBoard;

/**
 * @author Alon Segal
 *
 */
public class FullPathFitnessCalculator extends FitnessCalculator {

	/**
	 * 
	 */
	public FullPathFitnessCalculator(BoardEvaluator be) {
		super(be);
	}

	/* (non-Javadoc)
	 * @see evolution.FitnessCalculator#getFitness(evolution.Individual)
	 */
	@Override
	public double getFitness(Individual in) {
		
		double result = 0;
		//the sequence of moves in the individual
		Move[] moveSeq = in.getMoveSeq();
		//the scores of the board evaluation function in every state of the individual
		double[] scores = new double[moveSeq.length];
		
		CachingSmartBoard board = (CachingSmartBoard)in.getInitialBoard().clone();
		for(int i=0; i < moveSeq.length; i++) {
			board.makeMove(moveSeq[i]);
			scores[i] = be.evalBoard(board, in.getTurn());
		}
		//TODO: determine a better formula for the individual fitness
		double myFactor = 0;
		double opFactor = 0;
		for(int i=0; i < scores.length; i++) {
			if (i % 2 == 0) {
				myFactor = (scores.length/(i+1));
				result += myFactor * scores[i];
			} else {
				opFactor = (3/(i+1));
				//result -= opFactor * scores[i];
			}
		}

		return result;
	}

}
